<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备id" prop="did">
        <el-input
          v-model="queryParams.did"
          placeholder="请输入设备id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['radar:img:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table scrollbar-always-on v-loading="loading" :data="imgList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed="left" label="边坡名称" align="center" prop="slopeId" width="240px">
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeId"/>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="atTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.atTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图像最大纬度" align="center" prop="latmax" width="200" />
      <el-table-column label="图像最小纬度" align="center" prop="latmin" width="200" />
      <el-table-column label="图像最大经度" align="center" prop="lngmax" width="200" />
      <el-table-column label="图像最小经度" align="center" prop="lngmin" width="200" />
      <el-table-column label="设备id" align="center" prop="did"  width="200px"/>
      <el-table-column label="编码id" align="center" prop="dsId" />
      <el-table-column label="数据字段" align="center" prop="field" />
      <el-table-column label="图片类型" align="center" prop="fileType" />
      <el-table-column label="图片" fixed="right" align="center" prop="imgPath" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imgPath" :width="50" :height="50"/>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改雷达监测图片数据对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="imgRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Img">
import { listImg, getImg, delImg, addImg, updateImg } from "@/api/slope/video/radar/img";
import {listSlopeCode} from "@/api/slope/info/info.js";

const { proxy } = getCurrentInstance();
const { sys_dept_list } = proxy.useDict('sys_dept_list');

const slopeCodeList = ref([])
const imgList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    did: null,
    fileType: null,
    deptId: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询雷达监测图片数据列表 */
function getList() {
  loading.value = true;
  listSlopeCode().then(response => {
    // 假设 response 是一个普通的对象
    slopeCodeList.value = Object.entries(response).map(([key, value]) => ({
      label: key,
      value: value
    }))
  })
  listImg(queryParams.value).then(response => {
    imgList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    atTime: null,
    latmax: null,
    latmin: null,
    lngmax: null,
    lngmin: null,
    description: null,
    did: null,
    dsId: null,
    field: null,
    fileType: null,
    type: null,
    deviceId: null,
    imgPath: null,
    deptId: null
  };
  proxy.resetForm("imgRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.atTime);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加雷达监测图片数据";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _atTime = row.atTime || ids.value
  getImg(_atTime).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改雷达监测图片数据";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["imgRef"].validate(valid => {
    if (valid) {
      if (form.value.atTime != null) {
        updateImg(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addImg(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _atTimes = row.atTime || ids.value;
  proxy.$modal.confirm('是否确认删除雷达监测图片数据编号为"' + _atTimes + '"的数据项？').then(function() {
    return delImg(_atTimes);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('radar/img/export', {
    ...queryParams.value
  }, `img_${new Date().getTime()}.xlsx`)
}

getList();
</script>
